//
//  InstWatchViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/11.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "InstWatchViewController.h"
#import "WatchProblemViewController.h"

@interface InstWatchViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation InstWatchViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"趣味观察屋";
    [self creatData];
    UIImage *image = [UIImage imageNamed:@"ins_watch_bg"];
    self.view.layer.contents = (id) image.CGImage;
}
- (void)creatData{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestGetInterestPassList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray addObjectsFromArray: [NSMutableArray arrayWithArray:response[@"list"]]];
            [self creatMenu];
        }
    }];
}
- (void)creatMenu{
//    NSInteger menuLengt = 12;
    NSInteger num = 4;
    double width = 60;
    double margin = (kScreenW - num*60)/(num + 1);
    for (int i = 0; i <self.dataArray.count; i ++) {
        UIButton *menuButton = [[UIButton alloc]init];
        menuButton.frame = CGRectMake(margin + (margin + width) * (i % num), 100 + (margin + width) * (i / num), width, width);
        menuButton.tag = 1000 + i;
        [menuButton setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
        if ([self.dataArray[i][@"status"] intValue] == 1) {  //1 ：已解锁 0 未解锁
            [menuButton setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_select"] forState:UIControlStateNormal];
            [menuButton setTitle:[NSString stringWithFormat:@"%d",i+ 1] forState:UIControlStateNormal];
        }else{
            [menuButton setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_unselect"] forState:UIControlStateNormal];
        }
        menuButton.tag = 1000 + i;
        [menuButton addTarget:self action:@selector(addPassLog:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:menuButton];
    }
    
}
- (void)addPassLog:(UIButton *)sender{
    NSDictionary *dic = self.dataArray[sender.tag - 1000];
    if ([dic[@"status"] intValue] == 1) {  //1 ：已解锁进详情页 0 未解锁，调解锁接口
        WebViewController *webVC = [[WebViewController alloc]initWithTitle:dic[@"name"] withRequest:dic[@"url"]];
        [self.navigationController pushViewController:webVC animated:YES];
    }else{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message: [NSString stringWithFormat:@"解锁此关卡需要消耗%@积分",dic[@"value"]] preferredStyle:UIAlertControllerStyleAlert];
        
        // Create the actions.
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }];
        
        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
            [dic setValue:self.dataArray[sender.tag - 1000][@"id"] forKey:@"pass_id"]; //关卡id
            [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestAddPassLog] andParmas:dic andComplition:^(id response, BOOL isuccess) {
                if (isuccess) {
                    [sender setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_select"] forState:UIControlStateNormal];//self.dataArray[sender.tag - 1000][@"name"]
                    [sender setTitle:[NSString stringWithFormat:@"%ld",sender.tag - 1000 + 1] forState:UIControlStateNormal];
                    //解锁成功进详情页
                    WebViewController *webVC = [[WebViewController alloc]initWithTitle:@"趣味观察屋" withRequest:self.dataArray[sender.tag - 1000][@"url"]];
                    [self.navigationController pushViewController:webVC animated:YES];
                }
            }];
        }];
        
        // Add the actions.
        [alertController addAction:cancelAction];
        [alertController addAction:otherAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
- (void)enterWatch:(UIButton *)sender{
    WatchProblemViewController *watchProblemVC = [[WatchProblemViewController alloc]init];
    [self.navigationController pushViewController:watchProblemVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
